<?php

namespace App\Repository\Sale;

use App\Entity\Sale\PromotionProduct;
use App\Entity\Shop\ShopProduct;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;

/**
 * @method PromotionProduct|null find($id, $lockMode = null, $lockVersion = null)
 * @method PromotionProduct|null findOneBy(array $criteria, array $orderBy = null)
 * @method PromotionProduct[]    findAll()
 * @method PromotionProduct[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
 */
class PromotionProductRepository extends ServiceEntityRepository
{
    public $categoryRepository;
    public function __construct(ManagerRegistry $registry,PromotionCategoryRepository $categoryRepository)
    {
        parent::__construct($registry, PromotionProduct::class);
        $this->categoryRepository = $categoryRepository;
    }

    public function findOrCreate($id,ShopProduct $product)
    {
        $category = $this->categoryRepository->find($id);
        $pp = $this->findOneBy(array(
            "category"=>$category,
            "product"=>$product
        ));
        if (!$pp) {
            $pp = new PromotionProduct();
            $pp->setCategory($category)->setProduct($product);
            $this->_em->persist($pp);
            $this->_em->flush();
        }
    }


    // /**
    //  * @return PromotionProduct[] Returns an array of PromotionProduct objects
    //  */
    /*
    public function findByExampleField($value)
    {
        return $this->createQueryBuilder('p')
            ->andWhere('p.exampleField = :val')
            ->setParameter('val', $value)
            ->orderBy('p.id', 'ASC')
            ->setMaxResults(10)
            ->getQuery()
            ->getResult()
        ;
    }
    */

    /*
    public function findOneBySomeField($value): ?PromotionProduct
    {
        return $this->createQueryBuilder('p')
            ->andWhere('p.exampleField = :val')
            ->setParameter('val', $value)
            ->getQuery()
            ->getOneOrNullResult()
        ;
    }
    */
}
